package com.example.contactapp.adapters;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.PhoneLookup;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.contactapp.R;
import com.example.contactapp.objects.SmsObject;

@SuppressLint("SimpleDateFormat")
public class DetailSmsAdapter extends ArrayAdapter<SmsObject> {
	private OnClickListener myOnClickListener;
	private Context myContext;

	public DetailSmsAdapter(Context context, ArrayList<SmsObject> arrayContact) {
		super(context, R.layout.sms_detail_item, arrayContact);
		myContext = context;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(getContext()).inflate(
					R.layout.sms_detail_item, null);
			holder = new ViewHolder();
			holder.llReceive = convertView.findViewById(R.id.ll_receive);
			holder.llSend = convertView.findViewById(R.id.ll_send);

			holder.tvBodyReceive = (TextView) convertView
					.findViewById(R.id.tv_body_recieve);
			holder.tvDateReceive = (TextView) convertView
					.findViewById(R.id.tv_date_recieve);

			holder.tvBodySend = (TextView) convertView
					.findViewById(R.id.tv_body_send);
			holder.tvDateSend = (TextView) convertView
					.findViewById(R.id.tv_date_send);

			holder.imgRecieve = (ImageView) convertView
					.findViewById(R.id.img_recieve);
			holder.imgSend = (ImageView) convertView
					.findViewById(R.id.img_send);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		SmsObject contact = getItem(position);
		if (contact.getName() == null) {
			ContactObject object = getContactName(myContext, contact.getAddress());
			contact.setName(object.getName());
			if(object.getAvatar() != null){
				contact.setAvatar(object.getAvatar());
			}else{
				contact.setAvatar("");
			}
		}else{
			
		}
		if (contact.getType() == 1) {
			holder.llReceive.setVisibility(View.VISIBLE);
			holder.llSend.setVisibility(View.GONE);
			holder.tvBodyReceive.setText(contact.getBodySms());
			holder.tvDateReceive.setText(contact.getStrTime());
			/*if(contact.getAvatar().length() > 0){
				holder.imgRecieve.setImageURI(Uri.parse(contact.getAvatar()));
			}else{*/
				holder.imgRecieve.setImageDrawable(myContext.getResources().getDrawable(R.drawable.ic_contact_picture));
			//}
		} else {
			holder.llReceive.setVisibility(View.GONE);
			holder.llSend.setVisibility(View.VISIBLE);
			holder.tvBodySend.setText(contact.getBodySms());
			holder.tvDateSend.setText(contact.getStrTime());
			if(null != contact.getAvatar() && contact.getAvatar().length() > 0){
				holder.imgSend.setImageURI(Uri.parse(contact.getAvatar()));
			}else{
				holder.imgSend.setImageDrawable(myContext.getResources().getDrawable(R.drawable.ic_contact_picture));
			}
		}

		return convertView;
	}

	private static class ViewHolder {
		View llSend;
		View llReceive;
		TextView tvBodyReceive;
		TextView tvBodySend;
		TextView tvDateReceive;
		TextView tvDateSend;
		ImageView imgSend;
		ImageView imgRecieve;
	}

	public void setOnClickListener(OnClickListener onClickListener) {
		myOnClickListener = onClickListener;
	}

	public static interface OnClickListener {
		public void onClick(int position);
	}

	public String getDate(long milliseconds) {
		Date d = new Date(milliseconds);
		Calendar calNow = Calendar.getInstance();
		Calendar calYesterday = Calendar.getInstance();
		calYesterday.add(Calendar.DATE, -1);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

		String dateNow = dateFormat.format(calNow.getTime());
		String dateYesterday = dateFormat.format(calYesterday.getTime());
		String dateLog = dateFormat.format(d.getTime());

		if (dateLog.equals(dateNow))
			return "Today " + getHours(milliseconds);
		else if (dateLog.compareTo(dateYesterday) == 0)
			return "Yesterday" + getHours(milliseconds);
		Date date = new Date(milliseconds);
		SimpleDateFormat f = new SimpleDateFormat("MMM dd HH:MM");
		return f.format(date);
	}

	public String getHours(long milliseconds) {
		Date date = new Date(milliseconds);
		SimpleDateFormat f = new SimpleDateFormat("HH:MM");
		return f.format(date);
	}

	public ContactObject getContactName(Context context, String phoneNumber) {
		ContactObject object = new ContactObject();
		ContentResolver cr = context.getContentResolver();
		Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
				Uri.encode(phoneNumber));
		Cursor cursor = cr.query(uri, new String[] { PhoneLookup.DISPLAY_NAME,
				PhoneLookup.PHOTO_URI }, null, null, null);
		if (cursor == null) {
			return null;
		}
		String contactName = phoneNumber;
		if (cursor.moveToFirst()) {
			object.setName(cursor.getString(cursor
					.getColumnIndex(PhoneLookup.DISPLAY_NAME)));
			object.setAvatar(cursor.getString(cursor
					.getColumnIndex(PhoneLookup.PHOTO_URI)));

		} else {
			object.setName(phoneNumber);
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return object;
	}

	class ContactObject {
		private String name;
		private String avatar;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getAvatar() {
			return avatar;
		}

		public void setAvatar(String avatar) {
			this.avatar = avatar;
		}

	}
}
